package com.stationbuilding.utils;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

import com.stationbuilding.R;
import com.stationbuilding.activity.MainActivity;
import com.stationbuilding.activity.WelcomeActivity;

import static android.content.Context.NOTIFICATION_SERVICE;

public class MyReceiver extends BroadcastReceiver {
    private Context context;
    private PendingIntent pendingIntent; // 将要意图
    private static final String CHANNELID = "channelId"; // 8.0以上
    private static final String CHANNELNAME = "channelName"; // 8.0以上
    private static final String TICKER = "啦啦啦啦"; // 8.0以下
    private static final String CONTENTTITLE = "啦啦啦";
    private static final String CONTENTTEXT = "提醒";

    @Override
    public void onReceive(Context context, Intent intent) {

        this.context = context;


        pendingIntent = PendingIntent.getActivity(context,0,new Intent(context, WelcomeActivity.class),0);
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            createNotificationChannel(CHANNELID,CHANNELNAME,NotificationManager.IMPORTANCE_HIGH);
        }
            sendMsg();
        //因为AlarmManager 在高版本只可注册一次 这里进行再注册
        AlarmManagerUtils utils =  AlarmManagerUtils.getInstance(context);
        utils.createGetUpAlarmManager();
        utils.setStartTime(utils.getStartTime()+AlarmManagerUtils.TIME_INTERVAL);
        utils.getUpAlarmManagerStartWork();
    }
    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId,channelName,importance);
        channel.setShowBadge(true);
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        manager.createNotificationChannel(channel);
    }
    private void sendMsg(){
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            NotificationChannel channel = manager.getNotificationChannel(CHANNELID);
            if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE){
                Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE,context.getPackageName());
                intent.putExtra(Settings.EXTRA_CHANNEL_ID,channel.getId());
                context.startActivity(intent);
                Toast.makeText(context,"请手动将通知打开",Toast.LENGTH_SHORT).show();
            }
        }
        Notification notification = new NotificationCompat.Builder(context,CHANNELID)
                .setContentTitle(CONTENTTITLE)
                .setContentText(CONTENTTEXT)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .build();
        manager.notify(1,notification);
    }
}
